﻿using System.Diagnostics;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using zijian666.Converts;

namespace UnitTest
{
    [TestClass]
    public class UnitTest3
    {
        [TestInitialize]
        public void Before()
        {
            if (!Debugger.IsAttached)
            {
                _ = DiagnosticListener.AllListeners.Subscribe(new DiagnosticObserver());
            }
        }

        [TestMethod]
        public void 自定义强转()
        {
            var i = 11;
            var my = i.To<MyClass>();
            Assert.AreEqual(i, my.ID);
        }

        public class MyClass
        {
            public int ID { get; set; }
            public static explicit operator MyClass(int i) => new MyClass() { ID = i };
        }

        [TestMethod]
        public void 自定义隐转()
        {
            var my = new MyClass2 { ID = 100 };
            var i = my.To<int>();
            Assert.AreEqual(i, my.ID);
        }

        public class MyClass2
        {
            public int ID { get; set; }
            public static implicit operator int(MyClass2 my) => my?.ID ?? -1;
        }
    }
}
